package org.jypj.dev.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jypj.dev.model.Train;
import org.jypj.dev.model.TrainStation;
import org.jypj.dev.model.TrainWork;
import org.jypj.dev.model.User;
import org.jypj.dev.service.ITrainService;
import org.jypj.dev.service.ITrainStationService;
import org.jypj.dev.service.ITrainWorkService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.Page;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

/**
 * Train控制器
 *
 * @author ChenYu
 */
@Controller
@RequestMapping("trainController")
public class TrainController {

    @Resource
    private ITrainService trainService;

    @Resource
    private ITrainWorkService trainWorkService;

    @Resource
    private ITrainStationService trainStationService;


    @RequestMapping(value = "toTrainList.action")
    public String toTrainList() {

        return "back/train/train_main";
    }

    /**
     * 跳转到表单页面
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "toTrainForm.action")
    public String toTrainForm(String id, HttpServletRequest request) {
        request.setAttribute("id", id);
        return "back/train/train_form";
    }

    /**
     * 跳转到表单页面
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "toTrainFormCopy.action")
    public String toTrainFormCopy(String id, HttpServletRequest request) {
        request.setAttribute("id", id);
        return "back/train/train_form_copy";
    }

    /**
     * 查询所有数据接口
     *
     * @return code =-1 表示有异常 code=-2 表示没有数据 datas 菜单数据
     */
    @RequestMapping("selectAllTrain.action")
    @ResponseBody
    public Map<String, Object> selectAllTrain(String rid, String searchCode) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            queryMap.put("rid", rid);
            queryMap.put("code", searchCode);
            List<Train> trains = trainService.selectAllByMap(queryMap);
            map.put("datas", trains);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 查询所有的车次工序
     *
     * @param trainId
     * @return
     */
    @RequestMapping("selectAllTrainWork.action")
    @ResponseBody
    public Map<String, Object> selectAllTrainWork(String trainId) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            queryMap.put("trainId", trainId);
            List<TrainWork> trainWorks = trainWorkService.selectAllByMap(queryMap);
            List<TrainStation> trainStations = trainStationService.selectTrainStationsByTrainId(trainId);
            map.put("trainWorks", trainWorks);
            map.put("trainStations", trainStations);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 分页查询车次
     *
     * @param page
     * @return
     */
    @RequestMapping("selectOnePage.action")
    @ResponseBody
    public Page selectOnePage(Page page, Train train) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", train.getCode());
        page = trainService.selectOnePageByMap(page, map);
        return page;
    }

    /**
     * 保存或者更新
     *
     * @param trainStr
     * @return
     */
    @RequestMapping("saveOrUpdate.action")
    @ResponseBody
    public Map<String, Object> saveOrUpdate(String trainStr, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        String username = "";
        User user = (User) session.getAttribute("user");
        if (user != null) {
            username = user.getUsername();
        }
        String todayString = DateUtils.getTodayString("yyyy-MM-dd HH:mm:ss");
        int code = 0;
        Gson gson = new Gson();
        Train train = gson.fromJson(trainStr, Train.class);
        train.setCode(train.getCode().toUpperCase());
        train.setModifyTime(todayString);
        train.setModifyUser(username);
        if (StringUtil.isNotEmpty(train.getId())) {
            // 更新
            code = trainService.updateTrain(train);
        } else {
            // 保存
            code = trainService.saveTrain(train);
        }

        map.put("code", code);
        return map;
    }

    /**
     * 删除车次
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteTrain.action")
    @ResponseBody
    public Map<String, Object> deleteTrain(String id) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            trainService.deleteTrainById(id);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 按id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("selectObjectById.action")
    @ResponseBody
    public Map<String, Object> selectObjectById(String id) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            String todayString = DateUtils.getTodayString("yyyy-MM-dd");
            Train train = trainService.selectTrainById(id);
            String validDate = train.getValidDate();
            if (StringUtil.isEmpty(validDate)) {
                train.setValidDateShow(train.getModifyTime().substring(0, 10));
            } else {
                train.setValidDateShow(validDate);
                if (todayString.compareTo(validDate) >= 0) {
                    train.setValidDate("");
                }
            }
            List<TrainStation> trainStations = trainStationService.selectTrainStationsByTrainId(train.getId());
            train.setTrainStations(trainStations);
            map.put("data", train);
        } catch (Exception e) {
            e.printStackTrace();
            code = 0;
        }

        map.put("code", code);
        return map;
    }

}